//
//  SearchProgressTableView.m
//  danpin
//
//  Created by Hongyu Zhang on 15/11/12.
//  Copyright © 2015年 chuangjia. All rights reserved.
//

#import "SearchProgressTableView.h"
#import "viewconfig.h"
//#import "ZShare.h"
#import "SearchProgressTableviewCell.h"

@implementation SearchProgressTableView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void) resetData
{
    if(_baseData)
    {
        for (int i = 0; i < [_baseData count]; i++)
        {
            
            NSMutableDictionary * mdc = [_baseData objectAtIndex:i];
            
            if(mdc)
            {
                [mdc removeAllObjects];
            }
            
            mdc = nil;
        }
        
        [_baseData removeAllObjects];
    }

}
- (void)dealloc
{
    [self resetData];
    
    _baseData = nil;
    
    
    if(_actionBlock)
    {
        _actionBlock = nil;
    }
}


- (void) setComplete:(NSString*) strName price:(NSString*)strAvgPrice
{
    if(_baseData == nil)
        return;
    
    
    for (int i = 0; i < [_baseData count]; i++)
    {
        NSMutableDictionary* dc = [_baseData objectAtIndex:i];
        if( dc == nil)
            continue;
        
        NSString* name = [dc objectForKey:@"name"];
        
        if([name isEqual:strName])
        {
            [dc addEntriesFromDictionary:[NSDictionary dictionaryWithObject:strAvgPrice forKey:@"avg_price"]];
            break;
        }
        
    }
    
    [self reloadData];
    
}



- (void) setData:(NSArray*) ar reset:(BOOL) resetArray
{
    
    if(resetArray && _baseData)
    {
        [_baseData removeAllObjects];
        
        [self resetData];
        
        self.contentSize = CGSizeMake(0,0);
    }
    
    for (int i = 0; i < [ar count]; i++)
    {
        NSDictionary* dc = [ar objectAtIndex:i];
        if(dc && [dc isKindOfClass:[NSDictionary class]])
        {
            NSString * strName = [dc objectForKey:@"name"];//todo: 服务器应该有个name字段
            if(strName && [strName isKindOfClass:[NSString class]])
            {
              //  strName = [strName substringFromIndex:7];
              //  tagName.text =  strName;//dc objectForKey:@""
                if( [strName rangeOfString:@"相关资讯" options:NSCaseInsensitiveSearch].location == NSNotFound &&
                   [strName rangeOfString:@"晒单" options:NSCaseInsensitiveSearch].location == NSNotFound &&
                   [strName rangeOfString:@"百度" options:NSCaseInsensitiveSearch].location == NSNotFound &&
                   [strName rangeOfString:@"爆料" options:NSCaseInsensitiveSearch].location == NSNotFound )
                {
                    if(_baseData == nil)
                    {
                        _baseData = [[NSMutableArray alloc]init];
                    }
                    
                    NSMutableDictionary * mdc = [[NSMutableDictionary alloc]init];
                    [mdc addEntriesFromDictionary:dc];
                    [_baseData addObject:mdc];
                }
            }
        }
    }
    
    [self reloadData];
}

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 0.6, 0.6, 0.6, 0.5);
    CGContextSetLineWidth(ctx, 0.5);
    
    CGContextMoveToPoint(ctx, 0, 0);
    
    
    CGContextAddLineToPoint(ctx,0, self.bounds.size.height);
    
    
    CGContextStrokePath(ctx);
    
    
    
    //   self.lineView.frame =CGRectMake(0, self.bounds.size.height-0.5, DEVICE_WIDTH, 0.5);
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_baseData)
        return [_baseData count];
   
    return 0;
}


- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    view.tintColor = [UIColor clearColor];
}


- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    //  Background color
    view.tintColor = [UIColor clearColor];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getItemHeight:indexPath];
}

- (float) getItemHeight:(NSIndexPath *)indexPath
{
    if(_baseData && _baseData.count > 0)
    {
        float fNameWidth = 80;
        float fPriceWidth = 80;
        
        NSDictionary* dc = [_baseData objectAtIndex:indexPath.row];
        if(dc && [dc isKindOfClass:[NSDictionary class]])
        {
            NSString * strName = [dc objectForKey:@"name"];//todo: 服务器应该有个name字段
            if(strName && [strName isKindOfClass:[NSString class]])
            {
          //      cell.tagName.text =  strName;//dc objectForKey:@""
               CGSize sizeName = [strName boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(12)}context:nil].size;
//                CGSize sizeName = [strName sizeWithFont:FONT(12)
//                                      constrainedToSize:CGSizeMake(MAXFLOAT, 0.0)
//                                          lineBreakMode:NSLineBreakByWordWrapping];
                
                fNameWidth = sizeName.width + PHOTO_FRAME_WIDTH*2;
                
                if(fNameWidth < 70)
                {
                    fNameWidth = 70;
                }
                
            }
            
            
            NSString * strAvgPrice = [dc objectForKey:@"avg_price"];//todo: 服务器应该有个name字段
            if(strAvgPrice && [strAvgPrice isKindOfClass:[NSString class]])
            {
                if([strAvgPrice floatValue] > 0)
                {
                    strAvgPrice = [NSString stringWithFormat:@"¥%@", strAvgPrice];
                }
                else
                {
                    strAvgPrice = @"\\";
                }
                
              //  cell.tagValue.text =  strPrice;//dc objectForKey:@""
              //  strPrice =
            }
            else
            {
                strAvgPrice =  @"---";
            }
            CGSize sizeName  = [strAvgPrice boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT(12)}context:nil].size;
//            CGSize sizeName = [strAvgPrice sizeWithFont:FONT(12)
//                                  constrainedToSize:CGSizeMake(MAXFLOAT, 0.0)
//                                      lineBreakMode:NSLineBreakByWordWrapping];
            
            fPriceWidth = sizeName.width  + PHOTO_FRAME_WIDTH*2;
            
            if(fPriceWidth < 70)
            {
                fPriceWidth = 70;
            }
            
        }
        
        
        if(fPriceWidth > fNameWidth)
            return fPriceWidth;
        
        else
            return fNameWidth;
        
        
      //  return f;
    }
    
    return 80;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
 //   if(indexPath.section == 0)
 //   {
        SearchProgressTableviewCell * cell = (SearchProgressTableviewCell*)[tableView  dequeueReusableCellWithIdentifier:@"hotmall"];
        if(cell == nil)
        {
            cell = [[SearchProgressTableviewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hotmall"];
        //    cell.textLabel.font = FONT(13);
            cell.backgroundColor = [UIColor clearColor];
            cell.isline = YES;
         
            UILabel *tagName =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,
                                                                         PHOTO_TIME_PADDING,
                                                                        [self getItemHeight:indexPath] - PHOTO_FRAME_WIDTH*2,
                                                                         20)];
            tagName.backgroundColor = [UIColor clearColor];
            tagName.textColor = [UIColor grayColor];//UIColorFromRGB(0x000000);
            tagName.textAlignment = NSTextAlignmentCenter;
            tagName.font = FONT(12);
            
            
            [cell addSubview:tagName];
            cell.tagName = tagName;
            tagName=nil;
            
            

            UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,
                                                                         PHOTO_TIME_PADDING+20,
                                                                         [self getItemHeight:indexPath] - PHOTO_FRAME_WIDTH*2,
                                                                         20)];
            tagValue.backgroundColor = [UIColor clearColor];
            tagValue.textColor = [UIColor grayColor];//UIColorFromRGB(0x000000);
            tagValue.textAlignment = NSTextAlignmentCenter;
            tagValue.font = FONT(12);
            
            
            [cell addSubview:tagValue];
            cell.tagValue = tagValue;
            tagValue=nil;
            
            
            cell.transform = CGAffineTransformMakeRotation(M_PI / 2);
            
        }
    
    
    if(_baseData)
    {
        NSDictionary* dc = [_baseData objectAtIndex:indexPath.row];
        if(dc && [dc isKindOfClass:[NSDictionary class]])
        {
            NSString * strName = [dc objectForKey:@"name"];//todo: 服务器应该有个name字段
            if(strName && [strName isKindOfClass:[NSString class]])
            {
                cell.tagName.text =  strName;//dc objectForKey:@""
                
                cell.tagName.frame = CGRectMake(PHOTO_FRAME_WIDTH,
                                                PHOTO_TIME_PADDING,
                                                [self getItemHeight:indexPath] - PHOTO_FRAME_WIDTH*2,
                                                20);
            }
            
            
            NSString * strAvgPrice = [dc objectForKey:@"avg_price"];//todo: 服务器应该有个name字段
            if(strAvgPrice && [strAvgPrice isKindOfClass:[NSString class]])
            {
                if([strAvgPrice floatValue] > 0)
                {
                    strAvgPrice = [NSString stringWithFormat:@"¥%@", strAvgPrice];
                }
                else
                {
                    strAvgPrice = @"\\";
                }
                
                cell.tagValue.text =  strAvgPrice;//dc objectForKey:@""
                
                cell.tagValue.frame = CGRectMake(PHOTO_FRAME_WIDTH,
                                                 PHOTO_TIME_PADDING + 20,
                                                 [self getItemHeight:indexPath] - PHOTO_FRAME_WIDTH*2,
                                                 20);
            }
            else
            {
                strAvgPrice =  @"---";
                cell.tagValue.text =  @"---";
            }
            


        }
    }

    return cell;
    
//    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if(_actionBlock)
    {
        if(_baseData)
        {
            NSDictionary* dc = [_baseData objectAtIndex:indexPath.row];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString * strName = [dc objectForKey:@"name"];//todo: 服务器应该有个name字段
                if(strName && [strName isKindOfClass:[NSString class]])
                {
                    
                    _actionBlock(1, strName);
                }
                
            }
        }
        
        
    }
}


- (void) setCallback:(void(^)(int type, NSString* str)) callback
//(void(^actionBlock)(int type, NSString* str)) callback
{
    _actionBlock =  callback;
}

@end
